package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.annotation.OperationLog;
import com.mall.db.domain.Member;
import com.mall.db.domain.Role;
import com.mall.db.service.MemberService;
import com.mall.db.service.RoleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/mc")
public class MemberController extends BaseController<Member> {

    @Resource(name="memberService")
    private MemberService ms;

    @Resource(name="roleService")
    private RoleService rs;

    @Value("${file.root.photo}")
    private String root;


    @Override
    @OperationLog(value = "查询系统成员",type = 0)
    public Map<String, Object> find(Member member) {

        //实现分页
        PageHelper.startPage(member.getPage(), member.getLimit());

        QueryWrapper<Member> qw = new QueryWrapper<>();

        List<Member> list = ms.list(qw);

        PageInfo<Member> page = new PageInfo<>(list,member.getLimit());

        return this.findMap(page.getList(),page.getTotal());
    }



    @Override
    @OperationLog(value = "新增系统用户",type = 0)
    public Map<String, Object> add(Member member) {
        //TODO 密码加密
        member.setCreateTime((new Date()));
        boolean b = ms.save(member);
        return this.iud("新增",b,member);
    }

    @RequestMapping("/roleClass")
    public Map<String, Object> addRole( ModelMap modelMap){


        QueryWrapper<Role> qw = new QueryWrapper<>();
        List<Role> list = rs.list(qw);

        modelMap.addAttribute("list",list);
        return this.iud("查询",true,list);
    }


    @Override
    @OperationLog(value = "编辑系统成员",type = 0)
    public Map<String, Object> update(Member member) {

        boolean b = ms.updateById(member);
        return this.iud("编辑",b,member);
    }

    @RequestMapping("/editPwd")
    @OperationLog(value = "修改登录密码",type = 0)
    public Map<String,Object> editPwd(@RequestParam(name = "oldPwd",required = false) String oldPwd,
                                      @RequestParam(name = "newPwd") String newPwd,
                                      @RequestParam(name = "code") String code,
                                      @RequestParam(name = "id") int id,
                                      @RequestParam(name = "email",required = false) String email,
                                      HttpSession session){
        Map<String,Object> map = new HashMap<>();

        String mobileCode = session.getAttribute("mobileCode")+"";
        session.removeAttribute("mobileCode");
        if (!mobileCode.equals(code)){
            map.put("msg","验证码错误");
            map.put("is",false);
        }
        //邮箱修改密码
        Member m = null;
        if (StringUtils.isEmpty(id)) {
            //通过id获取对象
            m = ms.getById(id);
        }else {
            QueryWrapper<Member> qw = new QueryWrapper<>();
            qw.eq("id",id);
            qw.eq("email",email);
            m = ms.getOne(qw);
        }

        if (m!=null){
            if (StringUtils.isEmpty(email) && oldPwd.equals(m.getPassword())){
                m.setPassword(newPwd);//TODO 密码加密
                map.put("is",ms.updateById(m));
                map.put("msg","修改成功");
            }else if (!StringUtils.isEmpty(email)){
                m.setPassword(newPwd);//TODO 密码加密
                map.put("is",ms.updateById(m));
                map.put("msg","修改成功");
            }
            else {
                map.put("msg","旧密码输入错误！");
                map.put("is",false);
            }

        }else {
            map.put("msg","用户信息过期或不存在");
            map.put("is",false);
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(Member member) {

        Map<String, Object> map = new HashMap<>();

        if (!StringUtils.isEmpty(member.getId())){
            ms.removeById(member.getId());
            map.put("is",true);
            map.put("msg","删除成功！");
        }else {
            map.put("is",false);
            map.put("msg","删除失败！");
        }

        return map;
    }
}
